package com.jz.community.modulemine.model;

import android.app.Activity;
import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseText;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.JsonRequestBodyConverter;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.task.GetSmsCodeTask;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.modulemine.bean.ShareBean;
import com.jz.community.modulemine.bean.SignAwardBean;
import com.jz.community.modulemine.bean.SignDataBean;
import com.jz.community.modulemine.bean.SignResultBean;
import com.jz.community.modulemine.bean.UserChangeOtherBean;
import com.jz.community.modulemine.bean.UserInfoResultBean;
import com.jz.community.modulemine.bean.VersionBean;
import com.jz.community.modulemine.net.MineNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by Admin on 2018/3/28 0028.
 */

public class MineModelImp implements MineModel {


    private Context mContext;

    public MineModelImp(Context mContext) {
        this.mContext = mContext;
    }



    @Override
    public void getSmsCode(String mobi, String type, OnLoadListener<LoginInfo> onLoadListene) {
        new GetSmsCodeTask((Activity) mContext,false, o -> {
            if (o == null) {
                return;
            }
            onLoadListene.onSuccess((LoginInfo) o);
        }).execute(type, mobi);
    }


    @Override
    public void checkWifiRegister(String mobile, OnLoadListener<BaseResponseText> onLoadListene) {
        Observable<BaseResponseText> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .checkWifiRegister(mobile));
        new RxLoadingWrapper<BaseResponseText>(mContext)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListene.onSuccess(tbResultBean);
                }, throwable -> onLoadListene.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initSignData(OnLoadListener<SignDataBean> onLoadListener) {
        Observable<SignDataBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .memberSignData());
        new RxLoadingWrapper<SignDataBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code);
                    }
                });
    }

    @Override
    public void clickSign(String userId, OnLoadListener<SignResultBean> onLoadListener) {
        Map<String, String> params = new HashMap<>();
        params.put("platformId", "101");
        params.put("userId", userId);

        Observable<SignResultBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .clcikSign(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<SignResultBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void signAward(OnLoadListener<SignAwardBean> onLoadListener) {
        Observable<SignAwardBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .SignAward());
        new RxLoadingWrapper<SignAwardBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(ApiException.handleException(throwable).message, ApiException.handleException(throwable).code));
    }

    @Override
    public void updateVersion(String versionName, OnLoadListener<VersionBean> onLoadListener) {
        Observable<VersionBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .updateVersion(1, 1, versionName));
        new RxLoadingWrapper<VersionBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void memberLoginOut(String token, OnLoadListener<BaseResponseText> onLoadListene) {
        Observable<BaseResponseText> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .memberLoginOut(token));
        new RxLoadingWrapper<BaseResponseText>(mContext)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListene.onSuccess(tbResultBean);
                }, throwable -> onLoadListene.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void changePassword(String userId, String old_password, String password, OnLoadListener<UserChangeOtherBean> onLoadListener) {
        Observable<UserChangeOtherBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .changePassword(userId, old_password, password));
        new RxLoadingWrapper<UserChangeOtherBean>(mContext, false)
                .execute(observer)
                .subscribe(tbResultBean -> {
                    if (tbResultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(tbResultBean);
                }, throwable -> onLoadListener.onFail(ApiException.handleException(throwable).message, ApiException.handleException(throwable).code));
    }

    @Override
    public void unreadCount(String type, OnLoadListener<Integer> onLoadListener) {
        Observable<Integer> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .unreadCount(type));
        new RxLoadingWrapper<Integer>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initShareGift(OnLoadListener<ShareBean> onLoadListener) {
        Observable<ShareBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MineNetApi.class)
                .initShareGift());
        new RxLoadingWrapper<ShareBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
